# -*- coding: utf-8 -*-

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util

from datetime import timedelta

from gaep import *

class Test(object):
    def __str__(self):
        return  '%s [This is an Object.]' % object.__str__(self)


class MainHandler(webapp.RequestHandler):

    @session2(expire=timedelta(days=1))
    def get(self):
        f = self.request.get('f')
        if f=='set':
            self.session['str']='This is a string value.'
            self.session['dict']={'key':'This is a dictionary value'}
            self.session['list']=['Hello', 'world!', 123]
            self.session['object'] = Test()
            self.redirect('/')
        elif f == 'del':
            for k in self.session:
                del self.session[k]
                break
            self.redirect('/')
        elif f == 'des':
            self.session.destroy()
            self.redirect('/')
        else:
            self.show()

    def show(self):
        self.response.out.write("""
        <html>
        <head><title>Test</title></head>
        <body>
        <a href='/?f=set'>Set session data</a>|
        <a href='/?f=del' title='One by one.'>Delete session Data</a>|
        <a href='/?f=des'>Destroy session</a>
        <br />
        """)
        
        try:
            self.response.out.write('session_id=%s\n' % self.request.cookies['sessionid'])
        except:
            pass
        self.response.out.write('<pre>')
        for k in self.session:
            self.response.out.write('%s = %s \n' % (k,self.session[k]))
        self.response.out.write('</pre>')
        self.response.out.write('</body></html>')



def main():
    application = webapp.WSGIApplication([('/', MainHandler)],
                                         debug=True)
    util.run_wsgi_app(application)


if __name__ == '__main__':
    main()
